<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红色div固定在页面最上面</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .test {
            width: 800px;
            height: 100px;
            background: red;
            position: fixed;
            left: 50%;
            margin-left: -400px;
        }

        .out {
            width: 100%;
            height: 2000px;
            background-color: #abcdef;
            position: relative;
        }
    </style>
</head>

<body>
    <div class="out">
        <div class="test"></div>
    </div>
</body>

</html>